//
//  UIViewController+QXUtils.m
//  Pods
//
//  Created by Qianxia on 2016/12/22.
//
//

#import "UIViewController+QXUtils.h"

@implementation UIViewController (QXUtils)

/**
 添加到父视图
 
 @param parentViewController parentViewController description
 */
- (void)addToParentViewController:(UIViewController *)parentViewController {
    
    // 建立父子关系
    [parentViewController addChildViewController:self];
    [parentViewController.view addSubview:self.view];
    // 自动调整宽度和高度
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self didMoveToParentViewController:parentViewController];
}

- (void)addUseConstraintsToParentViewController:(UIViewController *)parentViewController {
    // 建立父子关系
    [parentViewController addChildViewController:self];
    [parentViewController.view addSubview:self.view];
    
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *viewsDictonary = @{@"view": self.view};
    [parentViewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:viewsDictonary]];
    [parentViewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:viewsDictonary]];
    
    [self didMoveToParentViewController:parentViewController];
}

/**
 显示界面
 
 @param parentViewController 从哪个界面弹出， 传nil则默认为window.rootViewController
 */
- (void)showWithParentViewController:(UIViewController *)parentViewController {
    
    if (!parentViewController) {
        UIResponder<UIApplicationDelegate> *delegate = [UIApplication sharedApplication].delegate;
        parentViewController = delegate.window.rootViewController;
    }
    
    parentViewController.providesPresentationContextTransitionStyle = YES;
    parentViewController.definesPresentationContext = YES;
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [parentViewController presentViewController:self animated:NO completion:nil];
}

@end
